{include file="public/header" title="收货地址管理"/}
<header class="mui-bar mui-bar-nav">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 class="mui-title">收货地址管理</h1>
    <!--<a href="" class="mui-btn-link mui-pull-right">明细</a>-->
</header>
<div class="mui-content layui-clear" id="my_address">

    {foreach name="list" id="value"}
    <div class="address-item layui-col-xs12">
        <h4>{$value['name']} <span>{$value['phone']}</span></h4>
        <p>{$value['city']}{$value['address']}</p>
        <p class="toolbar {if condition=" $value['default'] eq 1"}active{/if}">
        <input type="checkbox" onclick="check(this)" data-id="{$value['id']}" id="check" {if
               condition="$value['default'] eq 1" }checked{/if}/>
        <label for="check">设置为默认地址</label>
        <a onclick="address_del(this)" data-id="{$value['id']}"><i class="sun sun-delete1"></i>删除</a>
        <a href="{:url('my_address_edit',['id'=>$value['id']])}" data-id="1"><i class="sun sun-edit"></i>编辑</a>
        </p>
    </div>
    {/foreach}

    <div class="my-address-nav">
        <a href="{:url('my_address_add')}" class="layui-btn">新增收货地址</a>
    </div>
</div>
<script>
    (function ($) {
        $.init({
            swipeBack: true //启用右滑关闭功能
        });
    })(mui);
    var $ = layui.$, form = layui.form;

    /**
     * 删除收货地址
     * @param obj
     */
    function address_del(obj) {
        var id = $(obj).data('id');
        mui.confirm('确定要删除该地址吗？', '提示', ['取消', '确定'], function (response) {
            if (response.index > 0) {
                console.log(id);
                $.post("{:url('my_address_del')}", {id: id}, function (response) {
                    mui.toast(response.msg, {duration: 2000, type: 'div'});
                    if (response.code === 200) {
                        $(obj).parents('.address-item').remove();
                    }
                });
            }
        });
    }

    /**
     * 设置默认收货地址
     * @param obj
     */
    function check(obj) {
        var $checked = $(obj).is(':checked'), id = $(obj).data('id');
        $.post("{:url('my_address_default')}", {id: id}, function (response) {
            mui.toast(response.msg, {duration: 2000, type: 'div'});
            if (true === $checked) {
                $(obj).parents('.address-item').siblings('.address-item').find('.toolbar').removeClass('active').find('input').attr('checked', false);
                $(obj).parent().addClass('active');
            } else {
                $(obj).parent().removeClass('active');
            }
        });
    }

</script>
{include file="public/footer"/}